function R = bciTrainOneVsOneSVM(bci,T,L)


% create classifier pairs
R.classes = unique(L);
nClasses = length(R.classes);
nPairs = (nClasses-1)*nClasses/2;

k=0;
for k1=1:nClasses-1,
    for k2=k1+1:nClasses,
        k=k+1;
        R.classPair(k,:) = [k1,k2];
    end
end
    
R.W = cell(nPairs,1);
R.b0 = zeros(nPairs,1);

for k=1:nPairs;
    curIdx = (L==R.classes(R.classPair(k,1)) |L==R.classes(R.classPair(k,2)));
    curLabel = double(L(curIdx)==R.classes(R.classPair(k,1))) - ...
                double(L(curIdx)==R.classes(R.classPair(k,2)));
    if isfield(bci,'cspOvsOmask') && bci.param.CSPfilter>0,
        curTrain=T(curIdx,bci.cspOvsOmask==k);
    else
        curTrain=T(curIdx,:);
    end
    % train spider alg
    alg=svm;
    alg.optimizer='libsvm';
    if isempty(bci.param.movdirC),
            % determine default C according to Joachims
            alg.C=ecogGetDefC(curTrain);
    else 
            alg.C=bci.param.movdirC;
    end
    d = data(curTrain,curLabel);
    %evalc('[tr res_alg] = train(alg,d)');
    alg.algorithm.verbosity=0;
    [tr res_alg] = train(alg,d);
    R.W{k} = get_w(res_alg);
    R.b0(k) = res_alg.b0;
end
